package cc.orange;

import androidx.annotation.NonNull;

import cc.orange.BuildConfig;
import cc.orange.R;

import cc.orange.base.BaseActivity;
import cc.orange.entity.OKHttpUpdateHttpService;
import cc.orange.http.ApiService;
import cc.orange.mainView.MainA21Activity;
import cc.orange.mainView.MainActivity;
import cc.orange.utils.LoginsNotifyDialog;

import cc.orange.utils.WxLogUtil;
import cc.orange.utils.ZXToastUtil;
import cc.orange.utils.emojis.AndroidEmoji;
import cc.orange.utils.emojis.RongUtils;
import demo.smart.access.xutlis.util.ZXSharedPrefUtil;
import okhttp3.OkHttpClient;

import org.greenrobot.eventbus.EventBus;
import org.litepal.LitePal;
import org.xutils.x;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;

import com.tencent.bugly.crashreport.CrashReport;
//import com.tendcloud.tenddata.TalkingDataSDK;
import com.xuexiang.xupdate.XUpdate;
import com.xuexiang.xupdate.entity.UpdateError;
import com.xuexiang.xupdate.listener.OnUpdateFailureListener;
import com.zhy.http.okhttp.OkHttpUtils;

import android.os.Handler;
import android.view.View;

import java.util.concurrent.TimeUnit;

import static com.xuexiang.xupdate.entity.UpdateError.ERROR.CHECK_NO_NEW_VERSION;

public class SplashActivity extends BaseActivity {

    Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
//                    if (TextUtils.isEmpty(token_hr)) {
//                        startActivity(new Intent(SplashActivity.this, LonginWxActivity.class));
//                    } else {
                    initSDK();
                    if(BuildConfig.UIA_B.equals("A")){//默认A1
                        startActivity(new Intent(SplashActivity.this, MainA21Activity.class));
                    }else{// 1 是B面
                        startActivity(new Intent(SplashActivity.this, MainActivity.class));
                    }
                    finish();
                    break;
            }
        }
    };

//    private  String token_hr;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        if (!isTaskRoot()) {
             Intent intent = getIntent();
             String intentAction = intent.getAction();
            if (intent.hasCategory(Intent.CATEGORY_LAUNCHER) && intentAction != null &&
                    intentAction.equals(Intent.ACTION_MAIN)) {
                finish();
                return;
            }
        }
        ZXSharedPrefUtil sh = new ZXSharedPrefUtil();
        int isfirst = sh.getInt("isfirst", 0);
        if(isfirst == 1){
//            token_hr = sh.getString("token_hr");
            handler.sendEmptyMessageDelayed(1, 2000);
        }else{
            showDialogs();
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        if (handler != null) {
            handler.removeCallbacksAndMessages(null);
            handler = null;
        }
    }


    private LoginsNotifyDialog log_notify;

    protected void showDialogs(){
        log_notify = new LoginsNotifyDialog(this);
        log_notify.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(v.getId() == R.id.ln_cancle){
                    finish();
                }else if(v.getId() == R.id.ln_sure){
                    ZXSharedPrefUtil sh = new ZXSharedPrefUtil();
                    sh.putInt("isfirst", 1);
                    initSDK();
                    if(BuildConfig.UIA_B.equals("A")){//默认A1
                        startActivity(new Intent(SplashActivity.this, MainA21Activity.class));
                    }else{//B面
                        startActivity(new Intent(SplashActivity.this, MainActivity.class));
                    }
                    finish();
                }
            }
        });
        log_notify.showDialog();
    }


    private void initSDK(){
        WxLogUtil.initWx(getApplication(), BuildConfig.WX_AppId);
        ApiService.init();
        x.Ext.init(getApplication());
        LitePal.initialize(getApplication());
        AndroidEmoji.init(getApplication());
        RongUtils.init(getApplication());
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .connectTimeout(60, TimeUnit.SECONDS)
                .readTimeout(60, TimeUnit.SECONDS)
                .writeTimeout(60,TimeUnit.SECONDS)
                .build();
        OkHttpUtils.initClient(okHttpClient);

        CrashReport.initCrashReport(getApplicationContext(), "f956b30c98", true);
//        TalkingDataSDK.init(SplashActivity.this, BuildConfig.APP_TKey, BuildConfig.APP_CHANNEL, "a");

        XUpdate.get()
                .debug(true)
                .isWifiOnly(false)                                               //默认设置只在wifi下检查版本更新
                .isGet(true)                                                    //默认设置使用get请求检查版本
                .isAutoMode(true)                                              //默认设置非自动模式，可根据具体使用配置
//                .param("versionCode", UpdateUtils.getVersionCode(this))         //设置默认公共请求参数
//                .param("appKey", getPackageName())
                .setOnUpdateFailureListener(new OnUpdateFailureListener() {     //设置版本更新出错的监听
                    @Override
                    public void onFailure(UpdateError error) {
                        if (error.getCode() != CHECK_NO_NEW_VERSION) {          //对不同错误进行处理
                            ZXToastUtil.showToast(error.toString());
                        }
                    }
                })
                .supportSilentInstall(true)                                     //设置是否支持静默安装，默认是true
                .setIUpdateHttpService(new OKHttpUpdateHttpService())           //这个必须设置！实现网络请求功能。
                .init(getApplication());                                                    //这个必须初始化

    }
}